package com.zgxt.demo.controller;



import com.alibaba.fastjson.JSONObject;
import com.zgxt.demo.entity.DirectoryData;
import com.zgxt.demo.entity.ParentDirectory;
import com.zgxt.demo.entity.SecondDirectory;
import com.zgxt.demo.service.DirectoryService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class DirectoryController {




    @Resource
    private DirectoryService directoryService;


    @RequestMapping("/directory_data")
    public List<DirectoryData> DirectoryDataList(@RequestParam Integer pid){
        return directoryService.directoryList(pid);
    }

    @PostMapping("/second_directory_data")
    public List<DirectoryData> DirectoryDataListS(@RequestParam Integer sid){
        return directoryService.s_directoryList(sid);
    }


    @RequestMapping("/second_directory")
    public List<SecondDirectory> DirectoryDataListSecond(@RequestParam Integer pid){
        return directoryService.secondDirectoryList(pid);
    }

    @RequestMapping("/parent_directory")
    public List<ParentDirectory> DirectoryDataListParent() {
        return directoryService.parentDirectoryList();
    }

    @PostMapping("/directoryListAll")
    public Map<String,Object> DirectoryListAll(@RequestParam Integer page, @RequestParam Integer limit, String name){
        return directoryService.directoryListAll(page*limit,limit,name);
    }

    @PostMapping("/secondDirectoryListAll")
    public Map<String,Object> secondDirectoryListAll(@RequestParam Integer page, @RequestParam Integer limit, String name){
        return directoryService.secondDirectoryListAll(page*limit,limit,name);
    }

    @PostMapping("/s_directoryListAll")
    public Map<String,Object> s_directoryListAll(@RequestParam Integer page, @RequestParam Integer limit, String name){
        return directoryService.s_directoryListAll(page*limit,limit,name);
    }

    @PostMapping("/parentDirectoryListAll")
    public Map<String,Object> parentDirectoryListAll(@RequestParam Integer page, @RequestParam Integer limit, String name){
        return directoryService.parentDirectoryListAll(page*limit,limit,name);
    }

    @PostMapping("/addparent")
    public String addParent(@RequestParam String name){
        JSONObject _out=new JSONObject();
        if (directoryService.addParent(name)>0){
            _out.put("code",200);
            _out.put("msg","添加成功");
        }else {
            _out.put("code",500);
            _out.put("msg","添加失败");
        }
        return _out.toJSONString();
    }

    @PostMapping("/adddirectory")
    public String addDirectory(@RequestParam Integer pid,@RequestParam String title,@RequestParam String link){
        JSONObject _out=new JSONObject();
        if (directoryService.addDirectory(title,link,pid)>0){
            _out.put("code",200);
            _out.put("msg","添加成功");
        }else {
            _out.put("code",500);
            _out.put("msg","添加失败");
        }
        return _out.toJSONString();
    }

    @PostMapping("/addsecond")
    public String addSecond(@RequestParam Integer pid,@RequestParam String name){
        JSONObject _out=new JSONObject();
        if (directoryService.addSecond(name,pid)>0){
            _out.put("code",200);
            _out.put("msg","添加成功");
        }else {
            _out.put("code",500);
            _out.put("msg","添加失败");
        }
        return _out.toJSONString();
    }

    @PostMapping("/adds_directory")
    public String addS_Directory(@RequestParam Integer sid,@RequestParam String title,@RequestParam String link){
        JSONObject _out=new JSONObject();
        if (directoryService.addS_Directory(title,link,sid)>0){
            _out.put("code",200);
            _out.put("data","添加成功");
        }else {
            _out.put("code",500);
            _out.put("data","添加失败");
        }
        return _out.toJSONString();
    }

    @PostMapping("/deleteparent")
    public String deleteParent(@RequestParam String ids){
        JSONObject _out=new JSONObject();
        if (directoryService.deleteParent(ids)>0){
            _out.put("code",200);
            _out.put("msg","删除成功");
        }else {
            _out.put("code",500);
            _out.put("msg","删除失败");
        }
        return _out.toJSONString();
    }

    @PostMapping("/deletesecond")
    public String deleteSecond(@RequestParam String ids){
        JSONObject _out=new JSONObject();
        if (directoryService.deleteSecond(ids)>0){
            _out.put("code",200);
            _out.put("msg","删除成功");
        }else {
            _out.put("code",500);
            _out.put("msg","删除失败");
        }
        return _out.toJSONString();
    }

    @PostMapping("/deletes_directory")
    public String deleteS_Directory(@RequestParam String ids){
        JSONObject _out=new JSONObject();
        if (directoryService.deleteS_Directory(ids)>0){
            _out.put("code",200);
            _out.put("msg","删除成功");
        }else {
            _out.put("code",500);
            _out.put("msg","删除失败");
        }
        return _out.toJSONString();
    }

    @PostMapping("/deletedirectory")
    public String deleteDirectory(@RequestParam String ids){
        JSONObject _out=new JSONObject();
        if (directoryService.deleteDirectory(ids)>0){
            _out.put("code",200);
            _out.put("msg","删除成功");
        }else {
            _out.put("code",500);
            _out.put("msg","删除失败");
        }
        return _out.toJSONString();
    }

    @PostMapping("/updateparent")
    public String updateParent(@RequestParam Integer id,@RequestParam String name){
        JSONObject _out=new JSONObject();
        if (directoryService.updateParent(id,name)>0){
            _out.put("code",200);
            _out.put("msg","修改成功");
        }else {
            _out.put("code",500);
            _out.put("msg","修改失败");
        }
        return _out.toJSONString();
    }

    @PostMapping("/updatedirectory")
    public String updateDirectory(@RequestParam Integer id,@RequestParam Integer pid,@RequestParam String title,@RequestParam String link){
        JSONObject _out=new JSONObject();
        if (directoryService.updateDirectory(id,pid,title,link)>0){
            _out.put("code",200);
            _out.put("msg","修改成功");
        }else {
            _out.put("code",500);
            _out.put("msg","修改失败");
        }
        return _out.toJSONString();
    }

    @PostMapping("/updates_directory")
    public String updateS_Directory(@RequestParam Integer id,@RequestParam Integer sid,@RequestParam String title,@RequestParam String link){
        JSONObject _out=new JSONObject();
        if (directoryService.updateS_Directory(id,sid,title,link)>0){
            _out.put("code",200);
            _out.put("msg","修改成功");
        }else {
            _out.put("code",500);
            _out.put("msg","修改失败");
        }
        return _out.toJSONString();
    }

    @PostMapping("/updatesecond")
    public String updateSecond(@RequestParam Integer id,@RequestParam Integer pid,@RequestParam String name){
        JSONObject _out=new JSONObject();
        if (directoryService.updateSecond(id,name,pid)>0){
            _out.put("code",200);
            _out.put("msg","修改成功");
        }else {
            _out.put("code",500);
            _out.put("msg","修改失败");
        }
        return _out.toJSONString();
    }

    @PostMapping("/getParentBySid")
    public String getParentBySid(@RequestParam Integer sid){
        JSONObject _out=new JSONObject();
        _out.put("code",200);
        _out.put("msg","查询成功");
        _out.put("data",directoryService.getParentBySid(sid));
        return _out.toJSONString();
    }
}



